package com.hand.wfive.base

import android.content.Context
import android.util.Log
import com.alibaba.android.arouter.facade.annotation.Route
import com.alibaba.android.arouter.facade.service.SerializationService
import com.google.gson.Gson
import java.lang.reflect.Type
@Route(path = "/service/JsonServiceImpl")
class JsonServiceImpl : SerializationService {
    private var gson: Gson? = null
    override fun init(context: Context) {
        if (gson == null) {
            gson = Gson()
        }
    }

    override fun <T> json2Object(text: String, clazz: Class<T>): T? {
        return gson?.fromJson(text, clazz)
    }

    override fun object2Json(instance: Any): String {
        return try {
            gson?.toJson(instance).toString()
        } catch (e: Exception) {
            Log.e("object2Json:" , e.printStackTrace().toString())
            ""
        }
    }

    override fun <T : Any?> parseObject(input: String?, clazz: Type?): T? {
        return gson?.fromJson(input, clazz)
    }

}